package com.nami.logic.service.impl;

import com.nami.logic.cache.conver.ConverCache;
import com.nami.logic.service.ConverTaskService;
import com.nami.store.api.request.conversation.ConverItem;
import com.nami.store.api.service.RemoteConverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class StoreConverTaskServiceImpl extends AbstractConverSyncHandler {


    @Autowired
    public StoreConverTaskServiceImpl(RemoteConverService converService,
                                      ConverTaskService taskService) {
        super(converService, taskService);
    }

    @Override
    public void doHandler(String key) {
        Boolean ifPresent = TASK_CACHE.getIfPresent(key);
        if (null == ifPresent) {
            TASK_CACHE.put(key, Boolean.TRUE);
        }
    }

    @Override
    public boolean writeNow(String key) {
        ConverItem converItem = ConverCache.get(key);
        if (converItem == null) {
            log.warn("[conver] 同步会话出现错误，找不到会话，key={}", key);
            return false;
        }
        remoteConverService.writeCache(converItem);
        converTaskService.addAsyncWriteConverTask(buildTaskKey(converItem));
        return Boolean.TRUE;
    }

//    @Resource
//    public StoreConverTaskServiceImpl(RemoteConverService converService) {
//        super(converService);
//        this.converService = converService;
//    }


}

